import {
  getMsgList,
  confirmDelivery,
  startDelivery,
  letsGoDelivery,
  getUndoList,
  getMsgDetail,
  cancelDelivery
} from "../../utils/config"
import store from "../../utils/factory"
let that = null;
const util = require('../../utils/util.js')

Page({
  data: {
    showtab: 0, //顶部选项卡索引
    limit: 10,
    page: 1,
    noData: false,
    finish: false,
    status: "",
    pass: "off",
    needTime: true,
    submitStatus: false,
    tabnav: {
      tabnum: 5,
      tabitem: [{
          id: 1,
          text: "今日配送",
          status: "",
          pass: "off",
          needTime: true
        },
        {
          id: 2,
          text: "历史配送",
          status: "",
          pass: "off",
          needTime: false
        },
        {
          id: 3,
          text: "审核过滤",
          status: "",
          pass: "on",
          needTime: false
        },
        {
          id: 4,
          text: "未配送",
          status: "",
          pass: "",
          needTime: false,
        },
        {
          id: 5,
          text: "配送中",
          status: "配送中",
          pass: "off",
          needTime: false,
        },
        {
          id: 6,
          text: "已配送",
          status: "已配送",
          pass: "off",
          needTime: false,
        },
        {
          id: 7,
          text: "已拒绝",
          status: "已拒绝",
          pass: "off",
          needTime: false
        }
      ]
    },
    productList: [],
  },
  onLoad: function (e) {
    that = this;
    console.log(e.id)
    this.transferStauts(e.id)
  },
  transferStauts(status) {
    let a = status;
    switch (a) {
      case "1":
        this.setData({
          status: "",
          needTime: true,
          pass: "off",
          showtab: 0
        })
        break;
      case "2":
        this.setData({
          status: "",
          needTime: false,
          pass: "off",
          showtab: 1
        })
        break;
      case "3":
        this.setData({
          status: "",
          needTime: false,
          pass: "on",
          showtab: 2
        })
        break;
      case "4":
        this.setData({
          status: "",
          showtab: 3
        })
        break;
      case "5":
        this.setData({
          status: "配送中",
          needTime: false,
          pass: "off",
          showtab: 4
        })
        break;
      case "6":
        this.setData({
          status: "已配送",
          needTime: false,
          pass: "off",
          showtab: 5
        })
        break;
      case "7":
        this.setData({
          status: "已拒绝",
          needTime: false,
          pass: "off",
          showtab: 6
        })
        break;
      default:
        this.setData({
          status: "",
          showtab: 0,
          needTime: true,
          pass: "off",
        })
    }
  },
  onShow() {
    that = this;
    this.initPageStatus();
    console.log(this.data.showtab)
  },
  startDelivery(e) {
    if (this.data.submitStatus) {
      return false
    }
    this.setData({
      submitStatus: true
    })
    let orderDetail = e.currentTarget.dataset.obj;
    let index = e.currentTarget.dataset.index
    wx.showModal({
      content: "是否确认审核",
      showCancel: true,
      success: function (res) {
        that.initsubmitStatus()
        console.log(res)
        if (res.confirm) {
          wx.showLoading({
            title: "审核中..."
          })
          startDelivery({
            id: orderDetail.id
          }).then(res => {
            wx.hideLoading();
            wx.showToast({
              title: '审核配送成功',
              duration: 1000
            })
            getMsgDetail({
              id: orderDetail.id
            }).then(res => {
              let req = res.data;
              req.createTime = util.formatTime(new Date(req.createTime));
              let list = that.data.productList;
              list[index] = req
              that.setData({
                productList: list
              })
            });
          }).catch(err => {
            wx.hideLoading()
            that.initPageStatus()
          })
        }
      }
    })
  },
  initsubmitStatus() {
    that.setData({
      submitStatus: false
    })
  },
  initPageStatus() {
    this.setData({
      finish: false,
      page: 1,
      noData: false,
      submitStatus: false,
      productList: []
    })
    if (this.data.showtab == 3) {
      this.getUndoList()
    } else {
      this.queryOrder();
    }
  },
  letsGoDelivery(e) {
    if (this.data.submitStatus) {
      return false
    }
    this.setData({
      submitStatus: true
    })
    let orderDetail = e.currentTarget.dataset.obj;
    let index = e.currentTarget.dataset.index
    wx.showModal({
      content: "是否确认开始配送",
      showCancel: true,
      success: function (res) {
        that.initsubmitStatus()
        if (res.confirm) {
          letsGoDelivery({
            id: orderDetail.id
          }).then(res => {
            // that.initPageStatus();
            wx.showToast({
              title: '开始配送成功',
              duration: 1000
            })
            getMsgDetail({
              id: orderDetail.id
            }).then(res => {
              let req = res.data;
              req.createTime = util.formatTime(new Date(req.createTime));;
              let list = that.data.productList;
              list[index] = req
              that.setData({
                productList: list
              })
            });
          }).catch(err => {
            wx.hideLoading()
          })
        }
      }
    })
  },
  supplement(e) {
    let orderDetail = e.currentTarget.dataset.obj;
    wx.navigateTo({
      url: `../supplement/supplement?id=${orderDetail.id}`
    })
  },
  cancelDelivery(e) {
    if (this.data.submitStatus) {
      return false
    }
    this.setData({
      submitStatus: true
    })
    let orderDetail = e.currentTarget.dataset.obj;
    let index = e.currentTarget.dataset.index
    wx.showModal({
      content: "是否取消配送",
      showCancel: true,
      success: function (res) {
        that.initsubmitStatus()
        if (res.confirm) {
          cancelDelivery({
            id: orderDetail.id
          }).then(res => {
            wx.showToast({
              title: '取消配送成功',
              duration: 1000
            })
            let list = that.data.productList;
            list.splice(list.findIndex(item => item.id === orderDetail.id), 1)
            that.setData({
              productList: list
            })
          }).catch(err => {
            wx.hideLoading()
          })
        }
      }
    })
  },
  confirmDelivery(e) {
    if (this.data.submitStatus) {
      return false
    }
    this.setData({
      submitStatus: true
    })
    let orderDetail = e.currentTarget.dataset.obj;
    let index = e.currentTarget.dataset.index
    wx.showModal({
      content: "是否确认收货",
      showCancel: true,
      success: function (res) {
        that.initsubmitStatus()
        if (res.confirm) {
          confirmDelivery({
            id: orderDetail.id
          }).then(res => {
            wx.showToast({
              title: '确认收货成功',
              duration: 1000
            })
            getMsgDetail({
              id: orderDetail.id
            }).then(res => {
              let req = res.data;
              req.createTime = util.formatTime(new Date(req.createTime));
              let list = that.data.productList;
              list[index] = req
              that.setData({
                productList: list
              })
            });
          }).catch(err => {
            wx.hideLoading()
          })
        }
      }
    })
  },
  refund(e) {},
  getOrderDetail(e) {
    let edata = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../orderDetail/orderDetail?id=${edata.id}` // 页面 B
    })
  },
  setTab: function (e) {
    const edata = e.currentTarget.dataset;
    if (this.data.showtab == edata.tabindex) {
      return false
    }
    this.setData({
      showtab: edata.tabindex,
      productList: [],
      page: 1,
      noData: false,
      needTime: edata.needtime,
      pass: edata.pass,
      status: edata.status
    })
    if (this.data.showtab == 3) {
      this.getUndoList()
    } else {
      this.queryOrder()
    }
  },
  timeToStr(date) {
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    if (month < 10) {
      month = `0${month}`;
    }
    let day = date.getDate();
    if (day < 10) {
      day = `0${day}`;
    }
    return `${year}-${month}-${day}`;
  },
  queryOrder() {
    let time = this.timeToStr(new Date());
    getMsgList({
      page: this.data.page,
      limit: 10,
      status: this.data.status,
      pass: this.data.pass,
      time: this.data.needTime == true ? `${time}~2100-12-31` : ""
    }).then(res => {
      if (res.data.length == 0) {
        this.setData({
          finish: true,
          noData: true
        })
        return false
      }
      res.data.map(item => {
        item.createTime = util.formatTime(new Date(item.createTime));
      })
      if (res.data.length < 10) {
        this.setData({
          finish: true
        })
      }
      this.setData({
        productList: [...res.data, ...this.data.productList],
      })
    })
  },
  getUndoList() {
    getUndoList({
      page: this.data.page,
      limit: 10,
      status: "",
      pass: "",
    }).then(res => {
      if (res.data.length == 0) {
        this.setData({
          finish: true,
          noData: true
        })
        return false
      }
      res.data.map(item => {
        item.createTime = util.formatTime(new Date(item.createTime));
      })
      if (res.data.length < 10) {
        this.setData({
          finish: true
        })
      }
      this.setData({
        productList: [...res.data, ...this.data.productList],
      })
    });
  },
  onReachBottom() {
    console.log("bottom")
    if (this.data.finish) {
      return false
    }
    this.setData({
      page: this.data.page + 1
    })
    if (this.data.showtab == 3) {
      this.getUndoList()
    } else {
      this.queryOrder()
    }
  }
})